package net.sxnic.normal.util;

import java.io.File;
import java.io.IOException;
import java.util.List;

import net.sxnic.comm.CommConstant;
import net.sxnic.ugr.website.Website;
import net.sxnic.ugr.website.WebsiteManager;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebSiteImportUtils {

	private static Logger logger = LoggerFactory
			.getLogger(WebSiteImportUtils.class);

	public static void importWebSite(WebsiteManager wsManager, String filepath)
			throws IOException {
		File file = new File(filepath);
		List<String> lines = FileUtils.readLines(file, "utf-8");

		for (String line : lines) {
			
			
			if (StringUtils.isBlank(line)
					|| StringUtils.startsWith(line,
							CommConstant.TXT_IGNORE_CHAR)) {
				continue;
			}
						
			String[] wss = StringUtils.split(line, ",");
			Website ws = new Website();
			ws.setCode(wss[0]);
			ws.setName(wss[1]);
			ws.setLonginPage(wss[2]);
			ws.setMainPage(wss[3]);

			wsManager.save(ws);

			logger.debug("===save==website==success===" + ws.getCode() + "==="
					+ ws.getName());
		}
	}
}
